const app = getApp();
var api = require('../../../api.js');
var mta = require('../../../mta_analysis.js');

Page({
  data: {
    list:[],
    sum_integral: "",
    zero: "您还没有奖励，请加油！",
    judge: 0,
    pageCount: 1, //加载更多的分页数
    allPage: 0,  //总分页
  },
  onLoad: function (options) {
    app.bind();
    this.getRewardDetails();

    mta.Page.init();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {

    // app.bind();
    // this.getRewardDetails();
    
    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {

    var that = this;
    // 分页数
    var pageCount = that.data.pageCount;
    var allPage = that.data.allPage;
    pageCount ++;
    that.setData({
      pageCount: pageCount,
    })

    if (pageCount <= allPage) {
      that.getRewardDetails();
    } else {
      wx.showToast({
        title: '全部加载完成，不能加载更多',
        icon: 'none',
      })
    }
    
    // that.getMore();

  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },
  // 获取基本信息
  getRewardDetails: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var url = api.user.integralDetail;
    var pageCount = that.data.pageCount;
    url = url + "&page=" + pageCount;

    wx.request({
      url: url,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e);
        var allPage = e.data.data.page_count;
        var list = e.data.data.list;
        var listOrigin = that.data.list;
        var listLast = listOrigin.concat(list);

        if (e.data.code == 0) {

          if (e.data.data.row_count == 0) { // 数据为0时的选择
            that.setData({
              judge: 1,
            }) 

          } else {

            that.setData({
              allPage: allPage,
              list: listLast,
              sum_integral: e.data.sum_integral
            }) 

          }

        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  // getMore: function (e) {
  //       wx.showToast({
  //         title: 'getMore成功',
  //         icon: 'none',
  //         duration: 2000
  //       })
  // },
  explain: function (e) {
    console.log(e);
    wx.switchTab({
      url: '/pages/user/upgrade/upgrade'
    })
  },
  toUpgrade: function (e) {
    wx.switchTab({
      url: '/pages/user/upgrade/upgrade'
    })
  },
  toReceive: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '您升级之后，可以领取更多奖励！',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.receiveDetail(id);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  receiveDetail: function (id) {
    // console.log("——————————————————领取——————————————————————");
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var url = api.user.takeIntegral;
    console.log(id);
    wx.request({
      url: url,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        integral_id: id,
      },
      success: function (e) {
        console.log(e);
        wx.showToast({
          title: '领取成功！',
          icon: 'none',
        })
        that.getRewardDetails();
      }
    });
  },
})